<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>宿舍管理系统</title>
    <meta name="renderer" content="webkit">
    <meta name="csrf-token" content="{{ csrf_token() }}" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />

    <link rel="stylesheet" href="{{ asset('css/font.css') }}">
    <link rel="stylesheet" href="{{ asset('css/xadmin.css') }}">
    <script type="text/javascript" src="{{ asset('lib/layui/layui.js') }}" charset="utf-8"></script>
    <script type="text/javascript" src="{{ asset('js/xadmin.js') }}"></script>

    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                {{ csrf_field() }}
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>楼栋名称
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" required="" lay-verify="required"
                            autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>名称必须唯一
                    </div>
                </div>
                {{-- <div class="layui-form-item">
                    <label class="layui-form-label" for="company_id">所属公司</label>
                    <div class="layui-input-block">
                        <select name="company_id" lay-filter="company_id" lay-verify="required">
                            <option value="">无</option>
                            @foreach ($companies as $k => $v)
                                <option value="{{ $k }}">{{ $v }}</option>
                            @endforeach
                        </select>
                    </div>
                </div> --}}
                <div class="layui-form-item">
                    <label for="short_name" class="layui-form-label">简称
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="short_name" name="short_name" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">分区</label>
                    <div class="layui-input-block">
                        <input type="text" name="block" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">宿舍类型</label>
                    <div class="layui-input-block">
                        <select name="dorm_type_id" lay-verify="required">
                            <option value="">无</option>
                            @foreach ($dormTypes as $k => $v)
                                <option value="{{ $k }}">{{ $v }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="fee" class="layui-form-label">
                        <span class="x-red">*</span>单元名类型
                    </label>
                    <div class="layui-input-inline">
                        <input type="radio" name="unit_name_type" value="number" title="数字" checked>
                        <input type="radio" name="unit_name_type" value="letter" title="字母">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="fee" class="layui-form-label">
                        <span class="x-red">*</span>单元数量
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="unit_count" name="unit_count" required="" lay-verify="number|min1"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="fee" class="layui-form-label">
                        <span class="x-red">*</span>楼层数
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="floors" name="floors" required="" lay-verify="number|min1"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="fee" class="layui-form-label">
                        <span class="x-red">*</span>单元层房间数
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="rooms_per_floor" name="rooms_per_floor" required=""
                            lay-verify="number|min1" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="fee" class="layui-form-label">
                        <span class="x-red">*</span>每间可入住
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="beds_per_room" name="beds_per_room" required=""
                            lay-verify="number|min1" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>生成宿舍规则
                    </label>
                    <div class="layui-input-block">
                        <input type="text" name="gen_dorm_rule" value="{简称}-{单元}-{楼层}{流水号:00}"
                            lay-verify="required|gen_rule" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>如果规则为：{简称}-{单元}-{楼层}{流水号:00} ，那么A栋1单元1楼1号房生成的房号为：A-1-101
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <input type="text" name="remark" placeholder="请输入" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label">
                    </label>
                    <button class="layui-btn" lay-filter="add" lay-submit="">
                        添加
                    </button>
                </div>
            </form>
        </div>
    </div>
    <script>
        layui.use(['form', 'layer'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                    layer = layui.layer;

                //自定义验证规则
                form.verify({
                    gen_rule: function(rule) {
                        if (rule.trim().length == 0) {
                            return '生成规则不能为空';
                        } else {
                            if (rule.toString().indexOf('{简称}') == -1 && rule.toString().indexOf('{楼栋}') ==
                                -1) {
                                return '规则必须包含{简称}或{楼栋}其中一个';
                            }
                            if (rule.toString().indexOf('{楼层}') == -1) {
                                return '规则必须包含{楼层}';
                            }
                            if (rule.toString().search(/{流水号:0+}/g) == -1) {
                                return '规则必须包含{流水号:0}或{流水号:00}或{流水号:000}';
                            }
                        }
                    }
                });
                // 自定义验证规则：最小值不能小于1
                form.verify({
                    min1: function(value) {
                        if (parseInt(value) < 1) {
                            return '数值不能小于1';
                        }
                    }
                });

                form.on('radio(sc-off)', function(data) {
                    $("[name='from_month']").attr('disabled', true)
                    $("[name='from_month']").removeAttr('lay-verify')
                    $("[name='to_month']").attr('disabled', true)
                    $("[name='to_month']").removeAttr('lay-verify')
                });

                form.on('radio(sc-on)', function(data) {
                    $("[name='from_month']").removeAttr('disabled')
                    $("[name='from_month']").attr('lay-verify', 'number')
                    $("[name='to_month']").removeAttr('disabled')
                    $("[name='to_month']").attr('lay-verify', 'number')
                });


                form.on('radio(lc-off)', function(data) {
                    $("[name='level_to']").attr('disabled', true)
                    $("[name='level_to']").removeAttr('lay-verify')
                    $("[name='level_from']").attr('disabled', true)
                    $("[name='level_from']").removeAttr('lay-verify')
                });

                form.on('radio(lc-on)', function(data) {
                    $("[name='level_to']").removeAttr('disabled')
                    $("[name='level_to']").attr('lay-verify', 'number')
                    $("[name='level_from']").removeAttr('disabled')
                    $("[name='level_from']").attr('lay-verify', 'number')
                });
                //监听提交
                form.on('submit(add)',
                    function(data) {
                        console.log(data.field);
                        //发异步删除数据
                        $.ajaxSetup({
                            headers: {
                                'X-CSRF-Token': $('input[name="_token"]').val()
                            }
                        });
                        let url = '/buildings';
                        $.ajax({
                            url: url,
                            data: data.field,
                            type: 'POST',
                            dataType: "json",
                            statusCode: {
                                401: function() {
                                    console.log("statusCode 401 called");
                                    window.location.reload();
                                },
                                449: function() {
                                    console.log("statusCode 449 called");
                                }
                            },
                            success: function() {
                                console.log('success')
                                layer.alert('添加成功', {
                                    icon: 6,
                                    time: 5000
                                }, function() {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口数据表进行刷新
                                    parent.layui.table.reload('test');
                                });
                            },
                            error: function(err) {
                                console.log(err);
                                layer.msg('保存失败', {
                                    icon: 2,
                                    time: 1000
                                });

                            }
                        });
                        //ajax 异步调用会发生再这个语句之后，返回false可以避免异步返回时页面发生了刷新
                        return false;
                    });


            });
    </script>
</body>

</html>
